/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package th.stripes.action;

import com.google.inject.Inject;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.cfg.Settings;
import org.hibernate.impl.SessionFactoryImpl;
import th.stripes.BaseActionBean;

/**
 *
 * @author fikri
 */
@UrlBinding("/view/dokumen/")
public class DocumentAction extends BaseActionBean{
    
    @Inject
    protected com.google.inject.Provider<Session> sessionProvider;
    
    private static Logger LOGGER = Logger.getLogger(DocumentAction.class);
    
    private JasperReport jasperReport;
    private JasperPrint jasperPrint;
    
    public Resolution view() {
        Connection conn = null;

        byte[] bytes = null;

        try {
            conn = getConnection();           
            
            Map jasperParameter = new HashMap();
            jasperReport = JasperCompileManager
                    .compileReport("C://Users//fikri//Works//reports//SuratPenerbanganJH.jrxml");
//            jasperPrint = JasperFillManager.fillReport(jasperReport,jasperParameter, new JREmptyDataSource());
            
            bytes = JasperRunManager.runReportToPdf(jasperReport, jasperParameter, conn);

            
        } catch (Exception ex){
            LOGGER.error(ex);
        }
        final byte[] b = bytes;

        return new StreamingResolution("application/pdf") {

            @Override
            public void stream(HttpServletResponse response) throws Exception {
//                response.setHeader("Cache-Control", "no-cache");
//                response.setHeader("Pragma", "no-cache");
//                response.setDateHeader("Expires", 0);
//                response.setContentType("application/pdf");
//                response.setHeader("Content-disposition", "inline; filename=test.pdf");
//                ServletOutputStream out = response.getOutputStream();
//                out.write(bytes);
//                out.flush();
//                out.close();
                InputStream inputStream = new ByteArrayInputStream(b);
                BufferedInputStream bis = new BufferedInputStream(inputStream);
                ByteArrayOutputStream bous = new ByteArrayOutputStream();
                
                int ch;
                while ((ch = bis.read()) != -1) {
                    bous.write(ch);
                }
                bous.writeTo(response.getOutputStream());
            }
        }.setFilename("surat_penerbangan");

    }
    
    private Connection getConnection() throws SQLException {
        Settings settings = ((SessionFactoryImpl) sessionProvider.get().getSessionFactory()).getSettings();
        Connection c = settings.getConnectionProvider().getConnection();      
        return c;
    }

    
}
